package cn.net.isir.service;

import cn.net.isir.common.TokenManager;
import cn.net.isir.core.BasicService;
import cn.net.isir.core.SetBasicDao;
import cn.net.isir.dao.RoleDao;
import cn.net.isir.entity.Role;
import cn.net.isir.entity.RolePermission;
import cn.net.isir.entity.User;
import cn.net.isir.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService extends BasicService<Role> {
    @SetBasicDao
    @Resource
    private RoleDao roleDao;
    @Resource
    private RolePermissionService rolePermissionService;

    /**
     * 查询当前用户所在组织机构(含)的所有子角色
     *
     * @param parameter 带有创建人的
     * @return 组织机构列表
     */
    @Override
    public PageInfo selectListByUser(Object parameter) {
        Role role = (Role) parameter;
        if (StringUtils.isNotBlank(role.getRoleName())) {
            try {
                role.setRoleName(URLDecoder.decode(role.getRoleName(), StandardCharsets.UTF_8.name()));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return roleDao.selectPageList(role, "selectList", role.getPageNo(), role.getPageSize());
    }

    public List<Role> selectListByUser(User user) {
        return roleDao.selectListByUserId(user);
    }

    @Override
    public int insert(Role parameter) {
        int result = super.insert(parameter);
        // 如果没有添加权限，则新增角色结束
        if (StringUtils.isBlank(parameter.getPermissions())) {
            return result;
        }
//        roleDao.insert(parameter);
        //增加权限关联
        String[] newPermissions = parameter.getPermissions().split(",");
        if (newPermissions.length > 0) {
            for (String newPermission : newPermissions) {
                RolePermission rp = new RolePermission();
                rp.setRoleId(parameter.getId());
                rp.setPermissionId(newPermission);
                rp.setCreateUser(TokenManager.getTokenId());
                rolePermissionService.insert(rp);
            }
        }
        return 1;
    }

    @Override
    public int update(Role parameter) {
        Role role = roleDao.selectByPrimaryKey(parameter.getId());
        if (role == null) {
            return 0;
        }

        //修改权限
        String[] newPermissions = parameter.getPermissions().split(",");
        if (newPermissions.length > 0) {
            // 获取新旧两个列表
            List<String> newPermissionList = Arrays.stream(newPermissions).collect(Collectors.toList());

            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            List<RolePermission> rolePermissionList = rolePermissionService.selectList(rolePermission);
            // 新加的
            for (String s : newPermissionList) {
                boolean isExist = false;
                for (RolePermission permission : rolePermissionList) {
                    if (permission.getPermissionId().equals(s)) {
                        isExist = true;
                        break;
                    }
                }
                if (!isExist) {
                    RolePermission rp = new RolePermission();
                    rp.setRoleId(parameter.getId());
                    rp.setPermissionId(s);
                    rp.setCreateUser(TokenManager.getTokenId());
                    rolePermissionService.insert(rp);
                }
            }
            // 去掉的
            for (RolePermission permission : rolePermissionList) {
                boolean isExist = false;
                for (String s : newPermissionList) {
                    if (s.equals(permission.getPermissionId())) {
                        isExist = true;
                        break;
                    }
                }
                if (!isExist) {
                    rolePermissionService.delete(permission.getId());
                }
            }
        }
        return super.update(parameter);
    }
}
